<template>
	<view class="body">
		<xk-header :headerbg="true" :back="true" title="缴费单"></xk-header>
		<view class="tabs-bar">
			<view v-for="(item, index) of tabs" :key="index" :class="tabIndex === index ? 'item item-a' : 'item'" @click="clickTab(index)" >{{item.title}}</view>
		</view>

		<view class="list-info">
			<swiper :current="tabIndex" class="list-swiper"
				:style="{height: scrollViewHeight + 'px'}"
				@change="(e)=>{this.tabIndex = e.target.current}">
				<!-- <checkbox-group @change="checkboxChange"> -->
					<swiper-item v-for="(tabItem, tabIndex) of tabs" :key="tabIndex">
						<scroll-view class="data-list" scroll-y="true"
							v-if="dataList[tabIndex] && dataList[tabIndex].length > 0"
							refresher-enabled="true" :refresher-triggered="tabs[tabIndex].triggered" :refresher-threshold="100"
							@scrolltolower="onScrollToLower()"  @refresherrefresh="onRefresh()">
							<view class="data-item" v-for="(item, index) of dataList[tabIndex]" :key="index">
								<view class="item-top">
									<!-- <checkbox :value="index" v-if="tabIndex===0"/> -->
									<view>合同编号:{{item.apply_no}}</view>
									<view :class="item.status==='TO_PAY' ? 'status' : ''">{{$dicUtil.PiPayNoticeStatus(item.status)}}</view>
								</view>
								<view class="middle-view">
									<view class="name">¥</view>
									<view class="amount">{{item.total_money | formatMoney}}</view>
									<view class="common-btn btn-sty2" @click="goDetail(item)">账单详情</view>
								</view>
								<view class="bottom-info-view">
									<view class="date">{{item.date}}</view>
									<view class="date">缴费单号：{{item.pay_id}}</view>
								</view>
								<view class="info-button-view">
									<view class="common-btn btn-sty2" @click="goAppeal(item)"
										v-if="item.status!=='APPEAL' &&
											item.status!=='FINANCE_CONFIRM' &&
											item.status!=='COMPLETE'">账单申诉</view>
									<view class="common-btn" @click="onConfirm(item)"
										v-if="item.status==='CUSTOMER_CONFIRM'">账单确认</view>
									<view class="common-btn" @click="onPay(item)"
										v-if="item.status==='TO_PAY'">缴费确认</view>
								</view>
							</view>
						</scroll-view>

						<view class="empty-view" v-if="!dataList[tabIndex] || dataList[tabIndex] <= 0">
							<image class="empty-icon" :src="(imgUrl+'/wx/empty_data_icon.png')"></image>
							<view>哎呀，该界面暂无内容~</view>
						</view>
					</swiper-item>
				<!-- </checkbox-group> -->
			</swiper>
		</view>

		<!-- <view class='placeholder-view' v-show="tabIndex===0"></view>
		<view class="bottom-view" v-show="tabIndex===0">
			<view>
				<view class="left-view">
					<view>合计:</view>
					<view class="pay-money">{{totalMoney | formatMoney}} </view>
					<view>元</view>
				</view>
				<view class="common-btn btn" @click="onPay()">立即缴费</view>
			</view>
		</view> -->
	</view>
</template>
<script>
	import config from '@/common/config.js'
	import moment from 'moment'

	export default {
		data() {
			return {
				imgUrl:this.$config.BASE_imgUrl,
				scrollViewHeight: 0,
				tabIndex: 0,
				tabs: [{
					title: "待缴费",
					start: 0,
					isLoad: false,	// 数据加载标识
					isReload: false,	// 重新载入标识
					triggered: false
				},{
					title: "已缴费",
					start: 0,
					isLoad: false,	// 数据加载标识
					isReload: false,	// 重新载入标识
					triggered: false
				}],
				totalMoney: 0,
				dataList: [[], []],
				selectIndexList: []
			};
		},
		onLoad(options) {
			this.initInfo()
		},
		onShow() {
			this.$util.getHeightWithTopEl(".tabs-bar").then(height => {
				this.scrollViewHeight = height - 10;
			});
		},
		methods:{
			initInfo() {
				// 判断是否重新加载，isReload为true时，清空对应的列表数据
				for (let index in this.tabs) {
					let tab = this.tabs[index]
					if (tab.isReload) {	// 重新加载数据
						this.dataList[index].splice(0, this.dataList[index].length);
						this.tabs[index].isReload = false
						this.tabs[index].start = 0
						this.tabs[index].isLoad = true
					}
				}

				this.$api.piPayNotice_getPiPayNoticeList().then(res => {
					console.warn("缴费通知列表：", res)
					for(let item of res.unDoneList) {
						this.dataList[0].push(item)
					}

					for(let item of res.completeList) {
						this.dataList[1].push(item)
					}

					// 设置所有加载标识
					for (let index in this.tabs) {
						this.tabs[index].isLoad = false

						setTimeout(() => {
							this.tabs[index].triggered = false
						}, 1000)
					}
				})
			},
			clickTab(index){
				this.tabIndex = index
			},
			checkboxChange(e) {
				console.warn("eee: ", e.detail.value)
				this.selectIndexList = e.detail.value

				let datas = this.dataList[0];
				this.totalMoney = 0;
				for (let item of this.selectIndexList) {
					this.totalMoney += parseInt(datas[item].money)
				}
			},
			goDetail(item) {
				let eventName = "pay-detail";
				this.$EventBus.$off(eventName)
				this.$EventBus.$on(eventName, item => {
					this.$EventBus.$off(eventName)

					this.updateItem(item)
				})
				uni.navigateTo({
					url: "/pagesC/pay-order/pay-order-detail?data="+JSON.stringify(item) + "&eventName="+eventName
				})
			},
			goAppeal(item) {
				let eventName = "pay-appeal";
				this.$EventBus.$off(eventName)
				this.$EventBus.$on(eventName, item => {
					this.$EventBus.$off(eventName)
					this.updateItem(item)
				})
				uni.navigateTo({
					url: "/pagesC/pay-order/pay-order-appeal?data="+JSON.stringify(item) + "&eventName="+eventName
				})
			},
			onConfirm(item) {
				this.$util.confirm({
					content: "确认账单无误？",
					confirmText: "账单确认"
				}).then(isConfirm => {

					if (isConfirm) {
						this.$util.post({
							url: "app/piPayNotice/confirm",
							data: {
								pay_id: item.pay_id
							}
						}).then(res => {
							this.$util.toast("帐单确认成功")
							this.updateItem(item)
						})
					}
				})
			},
			onPay(item) {
				this.$util.confirm({
					content: "确认账单已缴费？",
					confirmText: "缴费确认"
				}).then(isConfirm => {
					if (isConfirm) {
						this.$util.post({
							url: "app/piPayNotice/pay",
							data: {
								pay_id: item.pay_id,
								pay_voucher: ""	// 支付凭着截图暂时为空
							}
						}).then(res => {
							this.$util.toast("缴费确认成功")
							this.updateItem(item)
						})
					}
				})
				// if (this.selectIndexList.length <= 0) {
				// 	this.$util.toast("请选择缴费合同")
				// 	return;
				// }
				// this.$util.confirm({
				// 	content: "确认要进行缴费操作？",
				// 	confirmText: "确认缴费"
				// }).then((isConfirm)=>{
				// 	if(isConfirm) {
				// 		this.$util.toast("缴费成功")
				// 	}
				// })
			},
			updateItem(item) {
				this.$api.piPayNotice_getPiPayNoticeSingle({
					pay_id: item.pay_id
				}).then(res=>{
					for (let index in this.dataList[this.tabIndex]) {
						if (this.dataList[this.tabIndex][index].pay_id === res.pay_id) {
							for (let key in this.dataList[this.tabIndex][index]) {
								this.dataList[this.tabIndex][index][key] = res[key];
							}
							break;
						}
					}
				})
			},
			onScrollToLower(e) {
				// if (this.tabs[this.tabIndex].isLoad) return
				// this.tabs[this.tabIndex].isLoad = true
				// this.initInfo()
			},
			onRefresh() {
				if (this.tabs[this.tabIndex].isLoad) return
				this.tabs[this.tabIndex].isReload = true
				this.tabs[this.tabIndex].triggered = true
				this.initInfo()
			},
		}
	}
</script>

<style lang="scss">
.tabs-bar{
	display: flex;
	align-items: center;
	flex-direction: row;
	justify-content: space-around;
	padding: 20rpx 30rpx 0rpx 30rpx;
	background-color: #FFFFFF;
	.item{
		height: 65rpx;
		align-items: center;
		justify-content: center;
		font-size: 32rpx;
		color: #333333;
		border-bottom: 6rpx solid rgba(#2C72FF, 0);
	}
	.item-a {
		font-weight: bolder;
		border-bottom: 6rpx solid #2C72FF;
	}
}


.list-info {
	margin: 0 30rpx;
	.data-list{
		border-radius: 10rpx;
		height: 100%;
		.data-item{
			margin-top: 20rpx;
			background-color: #ffffff;
			flex-direction: column;
			padding: 20rpx;
			font-size: 28rpx;
			.img{
				width: 335rpx;
				height: 250rpx;
				border-radius: 10rpx;
			}
			.item-top {
				display: flex;
				:first-child {
					flex: 1;
				}
				:last-child {
					padding: 5rpx 15rpx;
					font-size: 24rpx;
					color: #FF3737;
					background-color: rgba($color: #FFBABA, $alpha: 0.16);
					border-radius: 5rpx;
				}
				.status {
					color: #FF772C;
					background-color: rgba($color: #FF972C, $alpha: 0.16);
				}
			}
			.middle-view{
				flex: 1;
				display: flex;
				align-items: center;
				padding: 20rpx 0rpx;
				.name{
					font-size: 32rpx;
					font-weight: bolder;
					color: #333333;
				}
				.amount{
					font-size: 48rpx;
					color: #FF3737;
					margin: 10rpx 0rpx;
					font-weight: bold;
					margin-left: 20rpx;
					flex: 1;
				}
			}
			.common-btn{
				height: 60rpx;
				width: 160rpx;
				font-size: 28rpx;
				margin: 0;
			}
			.bottom-info-view {
				display: flex;
				margin: 10rpx 0rpx;
				:first-child {
					flex: 1;
				}
			}
			.info-button-view {
				display: flex;
				flex-direction: row;
				justify-content: flex-end;
				margin-top: 20rpx;
				>view {
					margin-left: 20rpx;
				}
			}
		}
		.data-item-right {
			margin-right: 20rpx;
		}
	}

}
.placeholder-view {
	height: 150rpx;
}

.bottom-view {
	position: fixed;
	width: 750rpx;
	bottom: var(--window-bottom);

	>view {
		display: flex;
		flex: 1;
		align-items: center;
		background-color: #FFFFFF;
		padding: 35rpx 30rpx 35rpx 30rpx;

		.left-view {
			display: flex;
			flex: 1;

			.pay-money {
				color: #FF3737;
				margin-left: 10rpx;
				font-weight: bold;
			}

			:last-child {
				margin-left: 10rpx;
			}
		}
		.btn {
			width: 280rpx;
			height: 80rpx;
			padding: 0rpx;
			margin: 0rpx;
		}
	}
}

</style>
